<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        .pear-container {
            margin: 15px;
            width: calc(100vw - 30px);
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">全文搜索</label>
                <div class="layui-input-inline">
                    <input type="text" name="deptName" placeholder="请输入部门名称" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dept-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="dept-table" lay-filter="dept-table"></table>
    </div>
</div>

<script type="text/html" id="dept-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开全部
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠全部
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="reload">
        <i class="layui-icon layui-icon-refresh"></i>
        重载
    </button>
</script>

<script type="text/html" id="dept-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #2DBBF0 !important;
                              border: 1px solid #2DBBF0 !important;" lay-event="edit">
        修改
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #398FFF !important;
                              border: 1px solid #398FFF !important;" lay-event="info">
        详情
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
        删除
    </button>
</script>
<script type="text/html" id="dept-status">
    <input type="checkbox" name="status" value="{{d.deptId}}" lay-skin="switch" lay-text="启用|禁用" mid="{{d.deptId}}"
           lay-filter="dept-status" {{ d.status== '1' ? 'checked' : '' }}>
</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let common = layui.common;


        var insTb = treetable.render({
            treeColIndex: 1,//树形图标显示在第几列
            treeSpid: 0,//最上级的父级id
            treeIdName: 'deptId', //id字段的名称
            treePidName: 'parentId',//父级节点字段
            skin: 'line',
            // method:'post',
            // treeDefaultClose: true,
            toolbar: '#dept-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            elem: '#dept-table',
            url: '/my/dept/list',
            id: 'testReload',
            page: true,
            cols: [
                [
                    {type: 'checkbox'},
                    {title: '名称', field: 'deptName', minWidth: 200},
                    {title: '排序', field: 'sort'},
                    {title: '是否可用', field: 'status', templet: '#dept-status'},
                    {title: '创建时间', field: 'createTime', align: 'center', templet: '#dept-time'},
                    {title: '更新时间', field: 'updateTime', align: 'center', templet: '#dept-time'},
                    {title: '操作', templet: '#dept-bar', width: 220, align: 'center'}
                ]
            ]
        });

        table.on('tool(dept-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'info') {
                window.info(obj);
            }
        })
        // 刷新按钮
        window.refresh = function (param) {
            treetable.reload("#dept-table");
        }

        table.on('toolbar(dept-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'expandAll') {
                treetable.expandAll("#dept-table");
            } else if (obj.event === 'foldAll') {
                treetable.foldAll("#dept-table");
            } else if (obj.event === 'reload') {
                treetable.reload("#dept-table");
            }
        });
        // 部门状态
        form.on('switch(dept-status)', function (obj) {
            layer.confirm(obj.elem.checked ? '确定要启用该部门吗' : '确定要停用该部门吗', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                var status = obj.elem.checked ? 1 : 0;
                var data = {
                    deptId: obj.value,
                    status: status
                }
                let loading = layer.load();
                $.ajax({
                    url: '/my/dept/status',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'put',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {icon: 1, time: 3000}, function () {
                                layui.table.reload("#dept-table");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 3000});
                            window.refresh();
                        }
                    }
                })
            });
        });
        form.on('submit(dept-query)', function (data) { //模糊查询方法
            var keyword = data.field.deptName;
            treetable.search('#dept-table', keyword);
            return false;
        });
        // 新增部门
        window.add = function () {
            layer.open({
                type: 2,
                title: ['新增部门', 'font-size: 18px;'],
                shade: 0.5,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '500px'],
                content: '/my/dept/add/view',
                move: false
            });
        }
        // 修改部门
        window.edit = function (obj) {
            var data = obj.data;
            layer.open({
                type: 2,
                title: ['修改部门', 'font-size: 18px;'],
                shade: 0.5,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '500px'],
                content: '/my/dept/edit/view?deptId=' + data.deptId
            });
        }
        //详情查看
        window.info = function (obj) {
            parent.layer.open({
                title: ['部门详情', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/my/dept/detail/' + obj.data['deptId'],
            });
        }
        // 删除部门
        window.remove = function (obj) {
            var data = obj.data;
            layer.confirm('确定删除吗,此操作不能撤销！', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/my/dept/delete?deptId=" + data.deptId,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 1800}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800});
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>